//
//  StockGoodsContentViewController.m
//  quxiangtao
//
//  Created by xbmios on 2018/7/25.
//  Copyright © 2018年 shanhaijin. All rights reserved.
//

#import "StockGoodsContentViewController.h"
#import "GoodsPicView.h"
#import "GoodPriceView.h"
#import "MemberView.h"
#import "GoodsTitleView.h"
#import "GoodsSelectedView.h"
#import "StockGoodsRankView.h"
#import "GoodsDetailContentView.h"
#import "BootomBtnView.h"

@interface StockGoodsContentViewController () <SDCycleScrollViewDelegate, UIScrollViewDelegate>

@property (strong, nonatomic) UIView *navBgView;

@property (strong, nonatomic) UIScrollView *contentScrollView;

@property (strong, nonatomic) GoodsPicView *goodsPicView;
@property (strong, nonatomic) GoodPriceView *priceView;
@property (strong, nonatomic) MemberView *memberV;
@property (strong, nonatomic) GoodsTitleView *titleView;
@property (strong, nonatomic) GoodsSelectedView *selectedView;
@property (strong, nonatomic) StockGoodsRankView *rankView;
@property (strong, nonatomic) GoodsDetailContentView *goodsDetailView;
@property (strong, nonatomic) BootomBtnView *BtnView;

@property (strong, nonatomic) NSArray *goodsSelectedArr;

// 导航栏白色背景的透明度
@property (assign, nonatomic) CGFloat alpha;

@end

@implementation StockGoodsContentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    self.view.backgroundColor = Colorff0000;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    // 导航栏右侧按钮
    [self createRightBarButtonItems];
    
    [self createUI];
    [self createTopSameUI];
    [self createDifferenceUI];
    [self createRemainUI];
    
    [self.view layoutIfNeeded];
    
    
    CGFloat height = CGRectGetMaxY(self.goodsDetailView.frame) + jisuan(20);
    
    NSLog(@"%.2f==%.2f===%.2f===%.2f", self.view.height, self.contentScrollView.height, self.BtnView.height, height);
    self.contentScrollView.contentSize = CGSizeMake(0, height);
    
}

#pragma mark -收藏
- (void)storeUpBtnClick:(UIButton *)btn {
    
    NSLog(@"收藏");
 
}

#pragma mark -分享
- (void)shareBtnClick:(UIButton *)btn {
    
    NSLog(@"分享");
    
    NSDictionary *dict = @{
                           
                           @"thumbURL"   : @"navShareBG",
                           @"title"     : @"标题",
                           @"des"        : @"分享内容",
                           @"webpageUrl" : @"http://www.baidu.com"
                           
                           };
    
    [[CommonAction sharedInstance] sharedWithDict:dict withController:self];
    
}

#pragma mark -scrollviewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat alpha = scrollView.contentOffset.y / 300;
    
    self.alpha = alpha;
    
    if (alpha > 1) {
        
        alpha = 1;
        
    }
    
    if (scrollView == self.contentScrollView) {
        
        self.navBgView.backgroundColor = WhiteColor;
        self.navBgView.alpha = alpha;
        
    }
    
}

- (void)createDifferenceUI {
    
    __weak typeof(self) weakSelf = self;
    
    self.memberV = [[MemberView alloc] init];
    [self.contentScrollView addSubview:self.memberV];
    
    self.titleView = [[GoodsTitleView alloc] init];
//    self.titleView.titleStr = @"GUCCI 古驰 女士Ace休闲真皮镶钻版小白鞋一箭穿心图案";
    [self.contentScrollView addSubview:self.titleView];
    
    
    if (0) {
        
        // 未达到会员最高等级
        self.memberV.hidden = NO;
        
        [self.memberV mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.mas_equalTo(weakSelf.view.mas_left);
            make.right.mas_equalTo(weakSelf.view.mas_right);
            make.top.mas_equalTo(weakSelf.priceView.mas_bottom);
            make.height.mas_equalTo(jisuan(100));
            
        }];
        [self.titleView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.mas_equalTo(weakSelf.view.mas_left);
            make.right.mas_equalTo(weakSelf.view.mas_right);
            make.top.mas_equalTo(weakSelf.priceView.mas_bottom).with.offset(jisuan(100));
            make.height.mas_equalTo(jisuan(170));
        }];
        
        
    }else {
        
        // 已达到会员最高等级 隐藏不创建
        
        self.memberV.hidden = YES;
        
        [self.titleView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.mas_equalTo(weakSelf.view.mas_left);
            make.right.mas_equalTo(weakSelf.view.mas_right);
            make.top.mas_equalTo(weakSelf.priceView.mas_bottom);
            make.height.mas_equalTo(jisuan(170));
        }];
        
    }
    
}

- (void)createRemainUI {
    
    __weak typeof(self) weakSelf = self;
    
    self.selectedView = [[GoodsSelectedView alloc] initWithType:1 andArray:self.goodsSelectedArr];
    self.selectedView.parentVC = self;
    [self.contentScrollView addSubview:self.selectedView];
    
    self.rankView = [[StockGoodsRankView alloc] init];
    [self.contentScrollView addSubview:self.rankView];
    
    self.goodsDetailView = [[GoodsDetailContentView alloc] init];
    [self.contentScrollView addSubview:self.goodsDetailView];
    
    [self.selectedView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.view.mas_left);
        make.right.mas_equalTo(weakSelf.view.mas_right);
        make.top.mas_equalTo(weakSelf.titleView.mas_bottom).with.offset(jisuan(20));
        make.height.mas_equalTo(jisuan(240));
        
    }];
    
    [self.rankView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.view.mas_left);
        make.right.mas_equalTo(weakSelf.view.mas_right);
        make.top.mas_equalTo(weakSelf.selectedView.mas_bottom).with.offset(jisuan(20));
        make.height.mas_equalTo(jisuan(600));
    }];
    
    [self.goodsDetailView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.view.mas_left);
        make.right.mas_equalTo(weakSelf.view.mas_right);
        make.top.mas_equalTo(weakSelf.rankView.mas_bottom).with.offset(jisuan(20));
        make.height.mas_equalTo(jisuan(934));
    }];
    
}

- (void)createTopSameUI {
    
    __weak typeof(self) weakSelf = self;
    
    self.goodsPicView = [[GoodsPicView alloc] init];
    self.goodsPicView.imageArr = @[[UIImage imageNamed:@"questionTopBG"], [UIImage imageNamed:@"questionTopBG"], [UIImage imageNamed:@"questionTopBG"],[UIImage imageNamed:@"questionTopBG"],[UIImage imageNamed:@"questionTopBG"]];
    [self.contentScrollView addSubview:self.goodsPicView];
    
    self.priceView = [[GoodPriceView alloc] initType:1];
    [self.contentScrollView addSubview:self.priceView];
    
    [self.goodsPicView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.view.mas_left);
        make.right.mas_equalTo(weakSelf.view.mas_right);
        make.top.mas_equalTo(weakSelf.contentScrollView.mas_top);
        make.height.mas_equalTo(jisuan(750));
        
    }];
    [self.priceView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.view.mas_left);
        make.right.mas_equalTo(weakSelf.view.mas_right);
        make.top.mas_equalTo(weakSelf.goodsPicView.mas_bottom).with.offset(jisuan(1));
        make.height.mas_equalTo(jisuan(150));
        
    }];
    
}

- (void)createUI {
    
    __weak typeof(self) weakSelf = self;
    [self.view addSubview:self.contentScrollView];
    
    
    self.BtnView = [[BootomBtnView alloc] initWithType:1];
    self.BtnView.parentVC = self;
    [self.view addSubview:self.BtnView];
    
    
    [self.BtnView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.view.mas_left);
        make.right.mas_equalTo(weakSelf.view.mas_right);
        make.bottom.mas_equalTo(weakSelf.view.mas_bottom);
        make.height.mas_equalTo(jisuan(100));
   
    }];

    [self.contentScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.view.mas_left);
        make.right.mas_equalTo(weakSelf.view.mas_right);
        make.top.mas_equalTo(weakSelf.view.mas_top);
        make.bottom.mas_equalTo(weakSelf.BtnView.mas_top);
        
    }];

}

#pragma mark -导航栏右侧按钮
- (void)createRightBarButtonItems {
    
    UIButton *storeUpBtn = [[UIButton alloc] init];
    [storeUpBtn setImage:[UIImage imageNamed:@"navStoreUpBG"] forState:UIControlStateNormal];
    [storeUpBtn setImage:[UIImage imageNamed:@"navStoreUpBG"] forState:UIControlStateSelected];
    [storeUpBtn addTarget:self action:@selector(storeUpBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [storeUpBtn sizeToFit];
    UIBarButtonItem *storeUpItem = [[UIBarButtonItem alloc] initWithCustomView:storeUpBtn];
    
    UIButton *shareBtn = [[UIButton alloc] init];
    [shareBtn setImage:[UIImage imageNamed:@"navShareBG"] forState:UIControlStateNormal];
    [shareBtn setImage:[UIImage imageNamed:@"navShareBG"] forState:UIControlStateSelected];
    [shareBtn sizeToFit];
    [shareBtn addTarget:self action:@selector(shareBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *shareItem = [[UIBarButtonItem alloc] initWithCustomView:shareBtn];
    
    UIBarButtonItem *fixedSpaceBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    fixedSpaceBarButtonItem.width = jisuan(44);
    
    self.navigationItem.rightBarButtonItems = @[shareItem, fixedSpaceBarButtonItem,storeUpItem];

}

#pragma mark -lazy
- (UIScrollView *)contentScrollView {
    
    if (!_contentScrollView) {
        
        _contentScrollView = [[UIScrollView alloc] init];
        _contentScrollView.backgroundColor = Colorf2f2f2;
        
        _contentScrollView.showsHorizontalScrollIndicator = NO;
        _contentScrollView.showsVerticalScrollIndicator = NO;
        
        _contentScrollView.delegate = self;
        
        if (@available (iOS 11.0 , *)) {
            
            _contentScrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        
    }
    
    return _contentScrollView;
}
- (NSArray *)goodsSelectedArr {
    
    if (!_goodsSelectedArr) {
        
        _goodsSelectedArr = @[
                              @{
                                  
                                  @"title" : @"选择",
                                  @"des"   : @"颜色、尺寸",
                                  
                                  },
                              @{
                                  
                                  @"title" : @"配送",
                                  @"des"   : @"至北京市丰台区花乡京明商务楼A座4001",
                                  
                                  },
                              @{
                                  
                                  @"title" : @"说明",
                                  @"des"   : @"不支持7天无理由退换",
                                  
                                  },
                              
                              ];
        
    }
    
    return _goodsSelectedArr;
}

- (UIView *)navBgView {
  
    if (!_navBgView) {
        
        _navBgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.navigationController.navigationBar.frame.size.width, self.navigationController.navigationBar.frame.size.height + 20)];
        [self.navigationController.view insertSubview:_navBgView belowSubview:self.navigationController.navigationBar];
        
    }
    
    return _navBgView;
    
}

#pragma mark -设置
- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.hidden = NO;
//    self.tabBarController.tabBar.hidden = YES;
    
    UIImage *image = [[UIImage alloc] init];
    //设置导航栏背景图片为一个空的image，这样就透明了
    [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
    //去掉透明后导航栏下边的黑边
    [self.navigationController.navigationBar setShadowImage:image];
    //    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    self.navigationController.navigationBar.translucent = YES;
    
    
    self.navBgView.alpha = self.alpha;
}

- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    
//    self.navigationController.navigationBar.hidden = NO;
//    self.tabBarController.tabBar.hidden = NO;
    
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:nil];
    self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
    self.navigationController.navigationBar.translucent = NO;
    
//    [self.navBgView removeFromSuperview];
    self.navBgView.alpha = 0;
    
}



@end
